import { _decorator, Button, Component, Node } from 'cc';
import { NodeExtension } from '../../CCExtension/NodeExtension';
import { JJYYUIRes } from './JJYYUIRes';
import { PageBase } from '../UIPage/PageBase';
const { ccclass, property } = _decorator;


export class ControllerBase extends Component {
    private btnDicInstance: Map<string, Button> = new Map();
    private activeController:Boolean = false;

    private page:Node

    protected get BtnDicInstance(): Map<string, Button> { return this.btnDicInstance; }

    public init(page:PageBase): void {
        if(this.activeController)
            return

        NodeExtension.visitAllDescendants(this.node, (node: Node) => {
            this.detectUIRes(node);
        });

        this.page = page.node
        this.setControllerElement()
        this.activeController = true
    }

    private detectUIRes(node: Node): void {
        const resMark = node.getComponent(JJYYUIRes);
        if (resMark) {
            this.tryGetBtn(resMark);
        }
    }

    private tryGetBtn(resMark: JJYYUIRes): void {
        if (!this.btnDicInstance.has(resMark.node.name)) {
            const btn = resMark.getComponent(Button);
            if (btn) {
                this.btnDicInstance.set(resMark.node.name, btn);
            } else {
                console.error("指定节点上不存在Button,节点名称为:" + resMark.node.name);
            }
        }
    }

    protected setControllerElement(): void {
        console.error('初始化方法未被实现!');
    }

    public getID(): string {
        console.error('控制器ID并未实现!'+this.name);
        return "Empty";
    }

    public active(): void {
        this.node.active = false
    }

    public close(): void {
        this.node.active = true
    }

    public closePage():void{
        this.page.active = false
        this.node.active = false
    }
}